package com.snailhd.common.util;

import java.util.concurrent.*;

/**
 * @author hd
 * @date 2019-12-18 22:01
 */
public class ThreadPoolManager {

    private static volatile ThreadPoolManager instance;
    private ExecutorService pool;

    private ThreadPoolManager() {
        BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue();
        this.pool = new ThreadPoolExecutor(4, 8, 30L, TimeUnit.SECONDS, workQueue);
    }

    public static ThreadPoolManager getInstance() {
        if (instance == null) {
            Class var0 = ThreadPoolManager.class;
            synchronized(ThreadPoolManager.class) {
                if (instance == null) {
                    instance = new ThreadPoolManager();
                }
            }
        }

        return instance;
    }

    public void executeThread(Runnable run) {
        this.pool.execute(run);
    }

    public <V> Future<V> executeThread(Callable<V> call) throws Exception {
        Future<V> f = this.pool.submit(call);
        return f;
    }
}
